<template>
  <div id="table_container">
    <el-card style="margin-bottom: 5px; text-align: left">
      <el-input v-model="keyWord" placeholder="输入搜索内容" style="width: 200px;" @keyup.enter.native="handleFilter"></el-input>
      <el-select v-model="status" placeholder="状态" style="width: 90px;margin-left: 10px;">
        <el-option v-for="item in importanceOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="handleFilter()" style="margin-left: 10px;">
        查询
      </el-button>
      <el-button v-permission="['role:add']" class="create_btn" style="float: right;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </el-card>
    <el-card>
      <el-table
          :data="tableData"
          v-loading="loading"
          row-key="id"
          border>
        <el-table-column
            type="index"
            label="序号"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="name"
            label="角色名称">
        </el-table-column>
        <el-table-column
            prop="key"
            label="角色编码">
        </el-table-column>
        <el-table-column
            prop="sort"
            align="center"
            label="排序"
            width="50">
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态"
            width="70">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === true ? 'success' :'danger' " disable-transitions>
              {{ scope.row.status === true ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="更新时间"
            width="120">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.update_time }}</span>
          </template>
        </el-table-column>
        <el-table-column
            label="创建时间"
            width="120">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.create_time }}</span>
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            fixed="right"
            width="300"
            label="操作">
          <template slot-scope="scope">
            <el-button v-permission="['role:permission']" @click="handleChange(scope.row)" type="success" icon="el-icon-s-operation">配置权限</el-button>
            <el-button v-permission="['role:edit']" @click="handleEdit(scope.row)" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button v-permission="['role:delete']" @click="handleDelete(scope.row)" type="danger" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog title="新建" width="400px" @close="handleClose" :visible.sync="createDialogFormVisible">
      <el-form :model="form" style="text-align: left">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="角色编码" :label-width="formLabelWidth">
          <el-input v-model="form.key" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="form.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="createDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCreate">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑" width="400px" @close="handleClose" :visible.sync="editDialogFormVisible">
      <el-form :model="form" style="text-align: left">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="角色编码" :label-width="formLabelWidth">
          <el-input v-model="form.key" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input-number v-model="form.sort"></el-input-number>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <el-drawer
        title="配置权限"
        :before-close="handleDialogClose"
        :visible.sync="drawerDialog"
        custom-class="demo-drawer"
        ref="drawer">
      <div class="demo-drawer__content">
        <el-form :model="form">
          <el-tree
              ref="tree"
              :data="treeData"
              show-checkbox
              node-key="id"
              check-strictly
              default-expand-all
              check-on-click-node
              :expand-on-click-node="false"
              :default-checked-keys="defaultChecked"
              :props="defaultProps">
          </el-tree>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="drawerDialog=false">取 消</el-button>
          <el-button type="primary" @click="updatePermission">确 定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {createRole, deleteRole, getPermissionList, getRole, updateRole} from "@/api";

export default {
  name: "index",
  data() {
    return {
      loading: true,
      tableData: [],

      createDialogFormVisible: false,
      showDialogFormVisible: false,
      editDialogFormVisible: false,
      form: {
        name: undefined,
        key: undefined,
        sort: 1,
        description: undefined,
        status: true,
        menus: [],
      },
      formLabelWidth: '80px',
      keyWord: undefined,
      importanceOptions: [{label: '启用', value: 1}, {label: '停用', value: 0}],
      status: undefined,

      menuOptions: [],

      drawerDialog: false,

      treeData: [],
      defaultProps: {
        label: 'title'
      },
      defaultChecked: [],
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    getDataList(params) {
      getRole(params).then(res => {
        this.tableData = res.data
        setTimeout(() => {
          this.loading = false
        }, 1.5 * 500);
      })
    },
    handleClose() {
      this.form = {
        name: undefined,
        key: undefined,
        sort: 1,
        description: undefined,
        status: true,
      }
    },
    handleClick(row) {
      console.log(row);
    },
    handleFilter() {
      this.getDataList()
    },
    handleCreate() {
      this.createDialogFormVisible = true

    },
    handleDetail(row) {
      this.showDialogFormVisible = true
      this.loadDialogOptions()
      this.form = {...row}
    },
    handleEdit(row) {
      this.editDialogFormVisible = true
      this.form = {...row}
    },
    handleDelete(row) {
      deleteRole(row.id).then(res => {
        this.$message.success('删除成功！');
        // this.$router.go(0)
        this.getDataList()
      })

    },
    submitCreate() {
      let data = this.form
      createRole(data).then(res => {
        this.$message.success('新建成功！')
        this.createDialogFormVisible = false
        // this.$router.go(0)
        this.getDataList()
      })
    },
    submitUpdate() {
      let data = this.form
      updateRole(data).then(res => {
        this.$message.success('修改成功！')
        this.editDialogFormVisible = false
        // this.$router.go(0)
        this.getDataList()
      })
    },
    handleDialogClose(done) {
      done()
    },
    handleChange(row) {
      this.initPermissionList()
      this.form = {...row}
      this.defaultChecked = this.form.menus.concat(this.form.permissions)
    },
    initPermissionList() {
      getPermissionList().then(res => {
        this.treeData = res.data
        this.drawerDialog = true
      })
    },
    updatePermission() {
      // let chekNode = this.$refs.tree.getCheckedNodes()
      let formData = this.form
      // let chekKeys = this.$refs.tree.getCheckedKeys()

      // let zi = this.$refs.tree.getCheckedKeys()//返回选中子节点的key
      // let fu =this.$refs.tree.getHalfCheckedKeys()//返回选中子节点的父节点的key
      // formData.menus = zi.concat(fu)
      formData.menus = this.$refs.tree.getCheckedKeys()

      updateRole(formData).then(res => {
        this.$message.success('修改权限成功！')
        this.drawerDialog = false
        this.getDataList()
      })


    },


  },
}
</script>

<style scoped>
.demo-drawer__content .el-form {
  height: 80vh;
  overflow-y: scroll;
}

.demo-drawer__footer {
  margin-top: 15px;
}
</style>